package com.mdd.front.service.collect.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mdd.common.core.PageResult;
import com.mdd.common.dto.result.GoodsCollectionResultDto;
import com.mdd.common.entity.goods.GoodsCollect;
import com.mdd.common.enums.GoodsCategoryStatusEnum;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.collect.GoodsCollectMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.front.service.collect.IGoodsCollectService;
import com.mdd.front.validate.collect.GoodsCollectPageParam;
import com.mdd.front.validate.collect.GoodsCollectParam;
import com.mdd.front.vo.collect.GoodsCollectDetailVo;
import com.mdd.front.vo.collect.GoodsCollectListVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 服务收藏实现类
 */
@Service
public class GoodsCollectServiceImpl extends ServiceImpl<GoodsCollectMapper, GoodsCollect> implements IGoodsCollectService {

    @Resource
    GoodsCollectMapper goodsCollectMapper;


    /**
     * 服务收藏列表
     *
     * @return PageResult<GoodsCollectListVo>
     */
    @Override
    public PageResult<GoodsCollectListVo> list(GoodsCollectPageParam param) {
        Page<GoodsCollectionResultDto> page = new Page<>(param.getPageNo(), param.getPageSize());
        Page<GoodsCollectionResultDto> resultDtoPage = goodsCollectMapper.page(page, param.getUserId());
        if (resultDtoPage == null || CollectionUtils.isEmpty(resultDtoPage.getRecords())) {
            return PageResult.iPageHandle(page.getTotal(), page.getCurrent(), page.getSize(), Lists.newArrayList());
        }
        List<GoodsCollectListVo> list = new LinkedList<>();
        for (GoodsCollectionResultDto item : resultDtoPage.getRecords()) {
            GoodsCollectListVo vo = new GoodsCollectListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImage(UrlUtil.toAbsoluteUrl(item.getImage()));
            vo.setStatusName(GoodsCategoryStatusEnum.getMap().get(vo.getStatus()));
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }
        return PageResult.iPageHandle(resultDtoPage.getTotal(), resultDtoPage.getCurrent(), resultDtoPage.getSize(), list);
    }

    /**
     * 服务收藏详情
     *
     * @param id 主键参数
     * @return GoodsCollect
     */
    @Override
    public GoodsCollectDetailVo detail(Long id) {
        GoodsCollect model = goodsCollectMapper.selectOne(
                new QueryWrapper<GoodsCollect>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        GoodsCollectDetailVo vo = new GoodsCollectDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 服务收藏新增
     *
     * @param goodsCollectParam 参数
     */
    @Override
    public void add(GoodsCollectParam goodsCollectParam) {
        LambdaQueryWrapper<GoodsCollect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsCollect::getGoodsId, goodsCollectParam.getGoodsId());
        lambdaQueryWrapper.eq(GoodsCollect::getUserId, goodsCollectParam.getUserId());
        lambdaQueryWrapper.last("limit 1");
        GoodsCollect one = super.getOne(lambdaQueryWrapper);
        if (null != one) {
            throw new OperateException("该服务已收藏");
        }
        GoodsCollect model = new GoodsCollect();
        model.setUserId(goodsCollectParam.getUserId());
        model.setGoodsId(goodsCollectParam.getGoodsId());
        model.setCreateTime(TimeUtil.timestamp());
        goodsCollectMapper.insert(model);
    }

    /**
     * 服务收藏编辑
     *
     * @param goodsCollectParam 参数
     */
    @Override
    public void edit(GoodsCollectParam goodsCollectParam) {
        GoodsCollect model = goodsCollectMapper.selectOne(
                new QueryWrapper<GoodsCollect>()
                        .eq("id", goodsCollectParam.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(goodsCollectParam.getId());
        model.setUserId(goodsCollectParam.getUserId());
        model.setGoodsId(goodsCollectParam.getGoodsId());
        goodsCollectMapper.updateById(model);
    }

    /**
     * 服务收藏删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Long id) {
        GoodsCollect model = goodsCollectMapper.selectOne(
                new QueryWrapper<GoodsCollect>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        goodsCollectMapper.delete(new QueryWrapper<GoodsCollect>().eq("id", id));
    }

    @Override
    public GoodsCollect getByGoodsId(Long userId, Long goodsId) {
        LambdaQueryWrapper<GoodsCollect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsCollect::getUserId, userId);
        lambdaQueryWrapper.eq(GoodsCollect::getGoodsId, goodsId);
        lambdaQueryWrapper.last("limit 1");
        return super.getOne(lambdaQueryWrapper);
    }

}
